<template>
  <div class="app-container">
    <!--查询表单-->
    <el-form :inline="true" align="center">
      <el-form-item>
        <el-input
          v-model="searchObj.vaccineName"
          class="inline-input"
          placeholder="根据疫苗名称查询"
        />
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="searchObj.vaccineManufacturer"
          class="inline-input"
          placeholder="根据疫苗厂商查询"
        />
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="searchObj.vaccineClassification"
          class="inline-input"
          placeholder="根据疫苗种类"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>
        <el-button type="default" @click="resetData()">清空</el-button>
      </el-form-item>

    </el-form>
    <!-- 工具按钮 -->
    <div style="margin-bottom: 10px">
      <el-button type="danger" size="mini" @click="batchRemove()">批量删除</el-button>
    </div>
    <!-- 表格 -->
    <el-table :data="list " border stripe @selection-change="handleSelectionChange">
      <el-table-column type="selection" />
      <el-table-column v-if="false" width="70" align="center" label="#">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="vaccineName" label="疫苗名称" width="180" align="center" />
      <el-table-column prop="vaccineManufacturer" label="疫苗厂商" width="180" align="center" />
      <el-table-column prop="vaccineClassification" label="疫苗分类" width="180" align="center" />
      <el-table-column prop="gmtCreate" label="添加时间" width="180" align="center" />
      <el-table-column prop="gmtModified" label="上次修改时间" width="180" align="center" />
      <el-table-column label="操作" width="337" align="center">
        <template slot-scope="scope">
           <router-link v-if="roles==='医院管理员'" :to="'/hospital-vaccine/create/'+scope.row.id+'/1'">
            <el-button type="success" size="mini" icon="el-icon-edit">添加疫苗库存</el-button>
          </router-link>
          <div v-if="roles==='管理员'" style="float: left">
            <router-link :to="'/vaccine/edit/'+scope.row.id">
              <el-button type="primary" size="mini" icon="el-icon-edit">修改</el-button>
            </router-link>
          </div>
          <div v-if="roles==='管理员'" style="float: left">
            <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeById(scope.row.id)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
      style="padding: 30px 0; text-align: center;"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="changePageSize"
      @current-change="changeCurrentPage"
    />
  </div>
</template>
<script>
import vaccineApi from '../../api/vaccine'
import Cookies from 'js-cookie'
export default {
  data() {
    return {
      list: null, // 数据列表
      total: 0, // 总记录数
      page: 1, // 页码
      limit: 10, // 每页记录数
      searchObj: {}, // 查询条件
      multipleSelection: [], // 批量删除选中的记录列表ID,
      roles: ''
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.roles = Cookies.get('roles')
      // 调用api
      vaccineApi.pageList(this.page, this.limit, this.searchObj)
        .then(response => {
          this.list = response.data.rows
          this.total = response.data.total
        })
    },
    // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
    changePageSize(size) {
      this.limit = size
      this.fetchData()
    },
    // 改变页码，page：回调参数，表示当前选中的“页码”
    changeCurrentPage(page) {
      this.page = page
      this.fetchData()
    },
    // 重置表单
    resetData() {
      this.searchObj = {}
      this.fetchData()
    },
    // 根据id删除数据
    removeById(id) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        vaccineApi.removeById(id).then((response) => {
          this.fetchData()
          this.$message.success(response.message)
        }).catch(error => {
          console.log('error', error)
        })
      }).catch(error => {
        // 当取消时会进入catch语句:error = 'cancel'
        // 当后端服务抛出异常时：error = 'error'
        if (error === 'cancel') {
          this.$message.info('取消删除 ')
        }
      })
    },
    // 当多选选项发生变化的时候调用
    handleSelectionChange(selection) {
      console.log(selection)
      selection.forEach(item => {
        this.multipleSelection.push(item.id)
      })
    },
    // 批量删除作者
    batchRemove() {
      if (this.multipleSelection.length === 0) {
        this.$message.warning('请选择要删除的记录！')
        return
      }
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        vaccineApi.batchRemove(this.multipleSelection).then(response => {
          this.fetchData()
          this.$message.success(response.message)
        }).catch(error => {
          console.log('error', error)
        })
      }).catch(error => {
        // 当取消时会进入catch语句:error = 'cancel'
        // 当后端服务抛出异常时：error = 'error'
        if (error === 'cancel') {
          this.$message.info('取消删除 ')
        }
      })
    }
  }
}
</script>
